﻿using StackExchange.Profiling;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Optimization;
using MvcDemo.Controllers;

namespace MvcDemo
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //移除WebForm视图引擎
            RemoveWebFormEngines();

            RouteConfig.RegisterRoutes(RouteTable.Routes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //BundleTable.EnableOptimizations = true;
        }

        /// <summary>
        /// 为捕获路由及通用异常处理
        /// http://www.cnblogs.com/luminji/archive/2011/07/31/2122533.html
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void Application_Error(object sender, EventArgs e)
        //{
        //    Response.Clear();
        //    Exception exception = Server.GetLastError();
        //    HttpException httpException = exception as HttpException;

        //    RouteData routeData = new RouteData();
        //    routeData.Values.Add("controller", "Error");
        //    if (exception == null)
        //    {
        //        routeData.Values.Add("action", "Index");
        //    }
        //    else if (httpException == null)
        //    {
        //        routeData.Values.Add("action", "Index");

        //    }
        //    else
        //    {
        //        switch (httpException.GetHttpCode())
        //        {
        //            case 404:
        //                routeData.Values.Add("action", "HttpError404");
        //                break;
        //            case 500:
        //                routeData.Values.Add("action", "HttpError500");
        //                break;
        //            default:
        //                routeData.Values.Add("action", "General");
        //                break;
        //        }
        //    }
        //    // Pass exception details to the target error View.  
        //    routeData.Values.Add("error", exception.Message);
        //    // Clear the error on server.  
        //    Server.ClearError();
        //    // Call target Controller and pass the routeData.  
        //    IController errorController = new ErrorController();
        //    errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));

        //}



        /// <summary>
        /// 移除WebForm引擎视图
        /// </summary>
        void RemoveWebFormEngines()
        {
            var viewEngines = ViewEngines.Engines;
            var webFormEngines = viewEngines.OfType<WebFormViewEngine>().FirstOrDefault();
            if (webFormEngines != null)
            {
                viewEngines.Remove(webFormEngines);
            }
        }

        protected void Application_BeginRequest()
        {
            if (Request.IsLocal)//这里是允许本地访问启动监控,可不写
            {
                MiniProfiler.Start();

            }
        }

        protected void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }
    }
}
